package fr.labri.harmony.analysis.jsep.results;

import java.io.IOException;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import fr.labri.harmony.analysis.jsep.results.filter.Utils;

public class ProjectActivity {
	
    static Map<String,Pair<Long,Long>> project_periods = null;
	static List<Integer> active_periods = null;

	private static void setUpProjectDates() throws IOException, SQLException {
		project_periods = new  HashMap<String, Pair<Long,Long>>();
		ResultSet rs = DBConnection.fetchDataView();
		while (rs.next()){			
			String project = rs.getString("URL");
			long start = Long.parseLong(rs.getString("START"));
			long end = Long.parseLong(rs.getString("END"));
			project_periods.put(project, new Pair<Long, Long>(start,end));
		}
		rs.close();
		active_periods = new ArrayList<Integer>();
		for(int i = 0 ; i < Utils.generateDates().size(); i++)
			active_periods.add(0);
		for(String project : project_periods.keySet()) {
			long start = project_periods.get(project).getElement0()*1000;
			long end = project_periods.get(project).getElement1()*1000;
			int k=0;
			for(Pair<Long,Long> period : Utils.generateDates()) {
				//Le project a démarré pendant la période
				if(start < period.getElement1() && start > period.getElement0())
					active_periods.set(k, active_periods.get(k)+1);
				//Le project a démarré avant la période est ne s'est pas arrété avant
				else if(start < period.getElement0() && end > period.getElement0())
					active_periods.set(k, active_periods.get(k)+1);
				k++;
			}
		}
	}
	
	public static int getMonthsForProject(String project, long end_migration) {
		if(project_periods==null)
			try {
				setUpProjectDates();
			} catch (IOException | SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		return getNumberOfMonths(project_periods.get(project).getElement0(),end_migration);
	}
	
	static int getNumberOfMonths(long start, long end) {
		Calendar startCalendar = new GregorianCalendar();
		startCalendar.setTime(new Date(start*1000));
		Calendar endCalendar = new GregorianCalendar();
		endCalendar.setTime(new Date(end*1000));

		int diffYear = endCalendar.get(Calendar.YEAR) - startCalendar.get(Calendar.YEAR);
		int diffMonth = diffYear * 12 + endCalendar.get(Calendar.MONTH) - startCalendar.get(Calendar.MONTH);
		return diffMonth;
	}

	
	public static List<Integer>  getProjectPeriod() {
		if(active_periods==null)
			try {
				setUpProjectDates();
			} catch (IOException | SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		return active_periods;
	}

	public static long getStart(String project) {
		if(project_periods==null)
			try {
				setUpProjectDates();
			} catch (IOException | SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		return project_periods.get(project).getElement0();
	}
	
	public static long getEnd(String project) {
		if(project_periods==null)
			try {
				setUpProjectDates();
			} catch (IOException | SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		return project_periods.get(project).getElement1();
	}

}
